package hot_100;

public class 二叉搜索树中第K小的元素 {
    public static void main(String[] args) {

    }

    int s;
    int ans = 0;

    public int kthSmallest(TreeNode root, int k) {
        s = k;
        dfs(root);
        return ans;
    }

    public void dfs(TreeNode root) {
        if (root == null||s==0)
            return;
        dfs(root.left);
        s--;
        if (s == 0){
            ans = root.val;
            return;
        }
        dfs(root.right);
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
